package ru.tomsk.autovokzal.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ru.tomsk.autovokzal.db.timetable.provider.TimetableContentProvider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class DataBaseTools {

	private String tag = DataBaseTools.class.getCanonicalName();
	private Map<String, Integer> pointMap;
	private Map<String, Integer> routeMap;
	private Context context;

	public DataBaseTools(Context cntx) {
		context = cntx;
	}

	public ArrayList<TimeTableItem> fillTimeTableArray() {
		Log.i(tag, "Fill time table array");
		ContentResolver contentResolver = context.getContentResolver();
		Cursor time_tableQuery = contentResolver.query(
				TimetableContentProvider.CONTENT_URI, new String[] {
						TimetableContentProvider._ID,
						TimetableContentProvider.DAYOFFOLLOWING,
						TimetableContentProvider.DEPATURETIME,
						TimetableContentProvider.ENDPOINTID,
						TimetableContentProvider.STARTPOINTID,
						TimetableContentProvider.NUMBEROFROUTEID,
						TimetableContentProvider.PRICE }, null, null, null);
		int timeTableCountItems = time_tableQuery.getCount();
		Log.i(tag, "Read " + timeTableCountItems + " timetable items");
		ArrayList<TimeTableItem> timeTableArray = new ArrayList<TimeTableItem>(
				timeTableCountItems);
		time_tableQuery.moveToFirst();
		do {
			TimeTableItem tt = new TimeTableItem();
			tt.setId(time_tableQuery.getLong(time_tableQuery
					.getColumnIndex(TimetableContentProvider._ID)));
			tt.setDayOfFollowing(time_tableQuery.getString(time_tableQuery
					.getColumnIndex(TimetableContentProvider.DAYOFFOLLOWING)));
			tt.setDepatureTime(time_tableQuery.getString(time_tableQuery
					.getColumnIndex(TimetableContentProvider.DEPATURETIME)));
			tt.setEndPoint(time_tableQuery.getString(time_tableQuery
					.getColumnIndex(TimetableContentProvider.ENDPOINTID)));
			tt.setStartPoint(time_tableQuery.getString(time_tableQuery
					.getColumnIndex(TimetableContentProvider.STARTPOINTID)));
			tt.setNumberOfRoute(time_tableQuery.getString(time_tableQuery
					.getColumnIndex(TimetableContentProvider.NUMBEROFROUTEID)));
			tt.setPrice(time_tableQuery.getDouble(time_tableQuery
					.getColumnIndex(TimetableContentProvider.PRICE)));
			timeTableArray.add(tt);

		} while (time_tableQuery.moveToNext());
		Log.i(tag, "Fill time table array success");
		time_tableQuery.close();
		return timeTableArray;

	}

	public void jsonToTimeTable(InputStreamReader inputStreamReader) {
		// TODO Auto-generated method stub
		BufferedReader buff = new BufferedReader(inputStreamReader);
		StringBuffer strBuff = new StringBuffer();
		ContentResolver contentResolver = context.getContentResolver();
		int c;
		try {
			while ((c = buff.read()) != -1) {
				strBuff.append((char) c);
			}
			JSONArray array = new JSONArray(strBuff.toString());
			int jsonArraySize = array.length();
			for (int i = 0; i < jsonArraySize; i++) {
				JSONObject JSONItem = (JSONObject) array.get(i);
				ContentValues timeTableRow = new ContentValues();
				timeTableRow
						.put(TimetableContentProvider.NUMBEROFROUTEID,
								JSONItem.getString(TimetableContentProvider.NUMBEROFROUTEID));
				timeTableRow
						.put(TimetableContentProvider.STARTPOINTID,
								JSONItem.getString(TimetableContentProvider.STARTPOINTID));
				timeTableRow
						.put(TimetableContentProvider.ENDPOINTID, JSONItem
								.getString(TimetableContentProvider.ENDPOINTID));
				timeTableRow
						.put(TimetableContentProvider.DEPATURETIME,
								JSONItem.getString(TimetableContentProvider.DEPATURETIME));
				timeTableRow
						.put(TimetableContentProvider.DAYOFFOLLOWING,
								JSONItem.getString(TimetableContentProvider.DAYOFFOLLOWING));
				timeTableRow.put(TimetableContentProvider.PRICE,
						JSONItem.getString(TimetableContentProvider.PRICE));
				Log.i(tag,
						"Insert value to timeTable" + timeTableRow.toString());
				Log.i(tag,
						"Insert value URI="
								+ contentResolver.insert(
										TimetableContentProvider.CONTENT_URI,
										timeTableRow));

			}

			Log.d(tag, strBuff.toString());
		} catch (IOException e) {
			Log.e(tag, e.getMessage(), e);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
